////////////////////////////////////////////////////////////////////////////////
//
//  Zemoga Inc
//  Copyright 2011 Zemoga Inc
//  All Rights Reserved.
//
////////////////////////////////////////////////////////////////////////////////
package com.videojukebox.components
{
	import ca.newcommerce.youtube.feeds.VideoFeed;
	
	import com.videojukebox.application.AppController;
	import com.videojukebox.components.skins.SearchFormBase;
	import com.zemoga.util.youtube.YoutubeSearchVideoProxy;
	import com.zemoga.util.youtube.YoutubeSearchVideoProxyEvent;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	
	/**
	 * Class description here
	 * 
	 * @author	davidnino	<email>
	 */
	public class SearchForm extends SearchFormBase
	{
		//------------------------------------------------------------------------------
		//
		//  Constants
		//
		//------------------------------------------------------------------------------
		
		//--------------------------------------
		//  Public
		//--------------------------------------
		
		//--------------------------------------
		//  Private
		//--------------------------------------
		
		//------------------------------------------------------------------------------
		//
		//  Static Methods
		//
		//------------------------------------------------------------------------------
		
		////////////////////////////////////////////////////////////////////////////////
		//
		//  Constructor
		//
		////////////////////////////////////////////////////////////////////////////////
		
		public function SearchForm()
		{
			super();
			
			addEventListener(FlexEvent.CREATION_COMPLETE, creationComplete_handler);
		}
		
		//------------------------------------------------------------------------------
		//
		//  Variables
		//
		//------------------------------------------------------------------------------
		
		//--------------------------------------
		//  Public
		//--------------------------------------
		
		//--------------------------------------
		//  Private
		//--------------------------------------
		private var appController:AppController = AppController.getInstance();
		private var youtubeSearchVideoProxy:YoutubeSearchVideoProxy;
		
		
		//------------------------------------------------------------------------------
		//
		//  Properties (getters/setters)
		//
		//------------------------------------------------------------------------------
		
		//------------------------------------------------------------------------------
		//
		//  Overriden methods
		//
		//------------------------------------------------------------------------------
		
		//------------------------------------------------------------------------------
		//
		//  Methods
		//
		//------------------------------------------------------------------------------
		
		//--------------------------------------
		//  Public
		//--------------------------------------
		
		//--------------------------------------
		//  Private
		//--------------------------------------
		private function buidUI():void
		{
			btn_search.addEventListener(MouseEvent.CLICK, btn_search_handler);
		}
		
		private function addEventListeners():void
		{
			youtubeSearchVideoProxy = new YoutubeSearchVideoProxy();
			youtubeSearchVideoProxy.addEventListener(YoutubeSearchVideoProxyEvent.SEARCH_SUCCESS, searchSuccess_handler);
			youtubeSearchVideoProxy.addEventListener(YoutubeSearchVideoProxyEvent.SEARCH_FAIL, searchFail_handler);
		}
		
		//------------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//------------------------------------------------------------------------------
		protected function creationComplete_handler(event:FlexEvent):void
		{
			removeEventListener(FlexEvent.CREATION_COMPLETE, creationComplete_handler);
			addEventListeners();
			buidUI();
		}
		
		protected function btn_search_handler(event:MouseEvent):void
		{
			if(txt_search.text != "")
			{
				youtubeSearchVideoProxy.getVideos(txt_search.text);
			}
			else
			{
				Alert.show("check your video search query");
			}
		}
		
		protected function searchSuccess_handler(event:YoutubeSearchVideoProxyEvent):void
		{
			trace("search success result");
			
			appController.setVideoSeachResult(event.data);
		}
		
		protected function searchFail_handler(event:YoutubeSearchVideoProxyEvent):void
		{
			
		}
	}
}